﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Workflow.Runtime.Hosting;
using SharedWorkflows;
using Bukovics.Workflow.Hosting;
using System.Workflow.Runtime;
using System.Collections.ObjectModel;
using System.Data;
using System.Workflow.Activities;
using CommonLibrary.DataModel.Basic;

namespace WinformTest
{
    public static class GlobleVar
    {
        public static UserEntity mySelf;

        public static WorkflowRuntimeManager _workflowManager;
        public static WorkflowPersistenceService _persistence;
        public static BillServer _billServer;
        public static Dictionary<Guid, Workflow> _workflows
            = new Dictionary<Guid, Workflow>();
        public static Workflow _selectedWorkflow;

        public static DataTable GetAllWorkflowTable()
        {
            //ReadOnlyCollection<WorkflowInstance> os = _workflowManager.WorkflowRuntime.GetLoadedWorkflows();
            String sql = "select task_id,uniqueid,fqrcode from wf_task where state='未处理' and user_id=" + GlobleVar.mySelf.duty_id;
            String[][] tmpdata4task = CommonLibrary.DB.DBExecuteUtil.querySqlArray(sql);
            if (tmpdata4task == null) return null;

            DataTable dt = new DataTable("报销单");
            dt.Columns.Add("流程号", typeof(Guid));
            dt.Columns.Add("报销号", typeof(string));
            dt.Columns.Add("报销人ID", typeof(string));
            dt.Columns.Add("报销人", typeof(string));
            dt.Columns.Add("报销金额", typeof(decimal));
            dt.Columns.Add("当前流程步骤", typeof(string));
            dt.Columns.Add("财务最后批示", typeof(string));
            dt.Columns.Add("老板最后批示", typeof(string));
            dt.Columns.Add("出纳最后批示", typeof(string));
            dt.Columns.Add("最后批示", typeof(string));
            dt.Columns.Add("task_id", typeof(string));

            foreach (String[] o in tmpdata4task)
            {
                string curState =GetCurrentState(new Guid(o[1]));
                string sql2 = "select billid,userid,username,money,fremark,bremark,premark,lastremark from billmodel" +
                    " where  instanceid='" + o[1]+"'";
                string[][] tmpdata = CommonLibrary.DB.DBExecuteUtil.querySqlArray(sql2);
                if (tmpdata != null)
                {
                    BillModel BM = new BillModel();
                    BM.BillId = tmpdata[0][0];
                    BM.UserID = int.Parse(tmpdata[0][1]);
                    BM.UserName = tmpdata[0][2];
                    BM.Money = decimal.Parse(tmpdata[0][3]);
                    BM.Fremark = tmpdata[0][4];
                    BM.Bremark = tmpdata[0][5];
                    BM.Premark = tmpdata[0][6];
                    BM.LastRemark = tmpdata[0][7];
                    dt.Rows.Add(o[1], BM.BillId,o[2], BM.UserName, BM.Money, curState, BM.Fremark, BM.Bremark, BM.Premark, BM.LastRemark, o[0]);
                }
                else
                {
                    DataRow dr = dt.NewRow();
                    dr["流程号"] = o[1];
                    dr["当前状态"] = curState;
                    dt.Rows.Add(dr);
                }
            }
            return dt;
        }

        public static string GetCurrentState(Guid instanceId)
        {
            StateMachineWorkflowInstance stateInstance = 
                new StateMachineWorkflowInstance(_workflowManager.WorkflowRuntime, instanceId);
            return stateInstance.CurrentStateName;
        }

    }
}
